/*
 * data_queue.c
 *
 *  Created on: 2021.09.24
 *      Author: taoye
 */

/* Includes ------------------------------------------------------------------*/
#include "public_queue.h"
/* Private defines -----------------------------------------------------------*/



/* Public enum    -----------------------------------------------------------*/


/* Public Struct  -----------------------------------------------------------*/


/* Private Variable  ---------------------------------------------------------*/
//Pointer

//Array

//Const

/* Private function prototypes -----------------------------------------------*/

/*****************************************************************************
 * @brief 队列初始化
 * ex:
 * @par
 * None
 * @retval
 *****************************************************************************/
void DataQueueInit(queue_t *queue, uint8_t *buf, uint16_t buf_len)
{
    queue->len = 0;
    queue->buf_len = buf_len;
    queue->rPtr = 0;
    queue->wPtr = 0;
    queue->buf = buf;
}

/*****************************************************************************
 * @brief 队列清空
 * ex:
 * @par
 * None
 * @retval
 *****************************************************************************/
void DataQueueClear(queue_t *queue)
{
    queue->len = 0;
    queue->rPtr = 0;
    queue->wPtr = 0;
}

/*****************************************************************************
 * @brief 写入一个数据
 * ex:
 * @par
 * None
 * @retval 0成功，1失败
 *****************************************************************************/
uint8_t DataQueueWriteByte(queue_t *queue, uint8_t data)
{
    if (queue->len >= queue->buf_len) {
        return 1;
    }

    queue->buf[queue->wPtr] = data;
    queue->wPtr++;
    queue->wPtr %= queue->buf_len;
    queue->len++;

    return 0;
}

/*****************************************************************************
 * @brief 读出一个数据
 * ex:
 * @par
 * None
 * @retval 0成功，1失败
 *****************************************************************************/
uint8_t DataQueueReadByte(queue_t *queue, uint8_t *data)
{
    if (queue->len <= 0) {
        return 1;
    }

    *data = queue->buf[queue->rPtr];
    queue->rPtr++;
    queue->rPtr %= queue->buf_len;
    queue->len--;

    return 0;
}

/*****************************************************************************
 * @brief 写入一段数据
 * ex:
 * @par
 * None
 * @retval 0成功，1失败
 *****************************************************************************/
uint8_t DataQueueWrite(queue_t *queue, uint8_t *data, uint16_t len)
{
    if (queue->len + len > queue->buf_len) {
        return 1;
    }

    if (queue->wPtr + len > queue->buf_len) {
        memcpy(&queue->buf[queue->wPtr], data, queue->buf_len - queue->wPtr);
        memcpy(queue->buf, data + (queue->buf_len - queue->wPtr), len - (queue->buf_len - queue->wPtr));
        queue->wPtr = len - (queue->buf_len - queue->wPtr);
    } else {
        memcpy(&queue->buf[queue->wPtr], data, len);
        queue->wPtr += len;
    }

    queue->len += len;
    return 0;
}

/*****************************************************************************
 * @brief 读出一段数据
 * ex:
 * @par
 * None
 * @retval 0成功，1失败
 *****************************************************************************/
uint8_t DataQueueRead(queue_t *queue, uint8_t *data, uint16_t len)
{
    if (len > queue->len) {
        return 1;
    }

    if (queue->rPtr + len > queue->buf_len) {
        memcpy(data, &queue->buf[queue->rPtr], queue->buf_len - queue->rPtr);
        memcpy(data + (queue->buf_len - queue->rPtr), queue->buf, len - (queue->buf_len - queue->rPtr));
        queue->rPtr = len - (queue->buf_len - queue->rPtr);
    } else {
        memcpy(data, &queue->buf[queue->rPtr], len);
        queue->rPtr += len;
    }

    queue->len -= len;
    return 0;
}

/*****************************************************************************
 * @brief 获取当前存储的数据长度
 * ex:
 * @par
 * None
 * @retval
 *****************************************************************************/
inline uint16_t DataQueueGetLen(queue_t *queue)
{
    return queue->len;
}

/*****************************************************************************
 * @brief 获取当前队列的存储长度
 * ex:
 * @par
 * None
 * @retval 0成功，1失败
 *****************************************************************************/
inline uint16_t DataQueueGetBufLen(queue_t *queue)
{
    return queue->buf_len;
}




/******************* (C) COPYRIGHT 2021 CIQTEK Samuel *****END OF FILE****/


